
Download Project (Full Project)
Step-1
In this part i will show how to use Google Recaptcha Using ASP.NET MVC. First open visual studio then create a new project then select asp.net web application. Now select Empty then click ok. If you want you can change project name or solution name. Visual studio creates a default project for you. Now you have to create a controller name as “Captcha” under controller folder. In this controller contain an index method. Now create an Empty view for the index method. Then create a method name as FormSubmit. This method is “HttpPost”. When submit the form then this method is called. This method also takes secret key. Given bellow the controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft;
using System.Net;
namespace GoogleCaptcha1.Controllers
{
public class CaptchaController : Controller
{
//
// GET: /Captcha/
public ActionResult Index()
{
return View();
}
//need to write post here
[HttpPost]
public ActionResult FormSubmit()
{
//Validate Google recaptcha below
var response = Request["g-recaptcha-response"];
string secretKey = "6LffNi4UAAAAAPOmrneA-_KYwXFZmYleNAqJeDsB";
var client = new WebClient();
ViewData["Message"] = "Google reCaptcha validation success";
//Here I am returning to Index view:
return View("Index");
}
}
}
Step-2
View pass the value into controller then c# controller receive the value and captcha work. In this view pass the value use rezor syntax. Given bellow the view code:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
@using (Html.BeginForm("FormSubmit", "Captcha", FormMethod.Post))
{
<div class="g-recaptcha" data-sitekey="6LffNi4UAAAAAN6-VAiY1ku46Wl9fYlpeYFm2epi"></div>
<input type="submit" value="Submit" />
}
</div>
<span style=" font-size:20px;margin:20px 0;padding:20px;border:1px solid #ff0000">
@ViewData["Message"]
</span>
<script src='https://www.google.com/recaptcha/api.js' type="text/javascript"></script>
Step-3
Now build and run the project.